package com.maoxx.humanresourcemanage_service.controller;

import com.maoxx.humanresourcemanage_service.dto.ResponseDTO;
import com.maoxx.humanresourcemanage_service.entity.Role;
import com.maoxx.humanresourcemanage_service.service.RoleMenuService;
import com.maoxx.humanresourcemanage_service.service.RoleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @className: RoleController
 * @author: Maoxx
 * @date: 2024/11/16 22:30
 * @Version: 1.0
 * @description:
 */


@RestController
@RequestMapping("/role")
public class RoleController {


    @Resource
    private RoleService roleService;

    @Resource
    private RoleMenuService roleMenuService;

    @ApiOperation("新增角色信息")
    @PostMapping
    public ResponseDTO addOne(@RequestBody Role role) {
        return roleService.addOne(role);
    }

    @ApiOperation("修改角色信息")
    @PutMapping
    public ResponseDTO updateOne(@RequestBody Role role) {
        return roleService.updateOne(role);
    }


    @ApiOperation("查询所有角色信息")
    @GetMapping
    public ResponseDTO getList(@RequestParam(defaultValue = "1") Integer current,
                               @RequestParam(defaultValue = "10") Integer size,
                               String name) {
        return roleService.getList(current, size, name);
    }


    @ApiOperation("根据ID删除角色信息")
    @DeleteMapping("/{id}")
    public ResponseDTO deleteOne(@PathVariable Integer id) {
        return roleService.deleteOne(id);
    }

    @ApiOperation("批量删除角色信息")
    @DeleteMapping("/batch/{ids}")
    public ResponseDTO deleteBatch(@PathVariable List<Integer> ids) {
        return roleService.deleteBatch(ids);
    }


    @ApiOperation("数据导出接口")
    @GetMapping("/export")
    public ResponseDTO export(HttpServletResponse response) throws IOException {
        return this.roleService.export(response);
    }

    @ApiOperation("数据导入接口")
    @PostMapping("/import")
    public ResponseDTO imp(MultipartFile file) throws IOException {
        return this.roleService.imp(file);
    }

    @ApiOperation("为角色设置菜单")
    @PostMapping("/menu/{roleId}")
    public ResponseDTO setMenu(@PathVariable Integer roleId, @RequestBody List<Integer> menuIds) {
        return roleMenuService.setMenu(roleId, menuIds);
    }

    @ApiOperation("得到角色所属的菜单")
    @GetMapping("/menu/{roleId}")
    public ResponseDTO getMenu(@PathVariable Integer roleId) {
        return roleMenuService.getMenu(roleId);
    }
}
